/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////Author: Shahab.Alishahian@gmail.com SA	
///////////////Target:The following methods are here to handle all GAPI functions.
///////////////Version:1.1
///////////////CopyRight: 2014 Noranj Co.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//*************************************************************************************************************
function setAddUser(data){
	if(data!='undefined'){
		var request = gapi.client.jb.user.fb.get({
			'id':data.facebookId
		});
		 request.execute(function(jsonResp, rawResp) {
			if(jsonResp.code==-32099){
				request = gapi.client.jb.user.insert({
		  			 'address': data.address,
		  			 'age':data.age,
		  			 'city':$("#jcCity").val(),
		  			 'country':$("#jcCountry").val(),
		  			 'dateCreated':data.dateCreated,
		  			 'email':data.email,
		  			 'facebookId':data.facebookId,
		  			 'firstName':data.firstName,
		  			 'gender':data.gender,
		  			 'ipAddress':$("#jcIPAddress").val(),
		  			 'lastName':data.lastName,
		  			 'merchantId':data.merchantId 
		  		 });
		  		 request.execute(function(jsonResp, rawResp) {
		  			$("#jcUserId").val(jsonResp.id);
		  	  	  	console.log('Inserted a user.' + jsonResp.id);
		  	  	  	saveBoard();
		  	    });
	  	  	}
			else{
				$("#jcUserId").val(jsonResp.id);
				saveBoard();
		 }
	    });
	}
	
}
//*************************************************************************************************************
function addBoard(data){
	 var request = gapi.client.jb.board.insert({
	 'boardCategory':data.boardCategory,
	 'boardDescription':data.boardDescription,
	 'boardItem':data.boardItem,
	 'boardKeywords':data.boardKeywords,
	 'boardName':data.boardName,
	 'boardTitile':data.boardTitile,
	 'dateCreated':data.dateCreated,
	 'boardType':data.boardType,
	 //'friendIds':data.friendIds,
	 'merchantId':data.merchantId,
	 'userId':data.userId
	 });
	 request.execute(function(jsonResp, rawResp) {
		  $("#jcBoardId").val(jsonResp.id);
    });
}
//*************************************************************************************************************
function addBoardItemDiscussion(data){
	 var request = gapi.client.jb.board.addDiscussion({
	 'boardId':data.boardItemId,
	 'actionRequired':data.actionRequired,
	 'discussion':data.discussion,
	 'faceBookReference':data.faceBookReference,
	 'userId':data.userId,
	 'status': "NEW"
	 });
	 request.execute(function(jsonResp, rawResp) {
		 addFriendBoard(data.boardItemId,$("#jcfbid").val()); //TODO:SA:Change the fbId to 
   });
}
//*************************************************************************************************************
function loadConversations(boardId){
	if(boardId!='undefined'){
		var request = gapi.client.jb.board.listDiscussionByStatus({
			'boardId':boardId,
			'status':'APPROVED'
		});
		 request.execute(function(jsonResp, rawResp) {
			 if(jsonResp.items != undefined){
				 for (var i = 0; i < jsonResp.items.length; i++){
					 getContributer(jsonResp.items[i]);
				 }
		 	}
		 });
	}
}

//*************************************************************************************************************
function getContributer(data){
	var contributer = {};
	if(data!='undefined'){
		var request = gapi.client.jb.user.get({
			'id':data.userId
		});
		
		request.execute(function(jsonResp, rawResp) {
			contributer["id"] = jsonResp.id;
			contributer["firstName"] = jsonResp.firstName;
			contributer["lastName"] = jsonResp.lastName;
			contributer["facebookId"] = jsonResp.facebookId;
			$("#boardCommentList").append(createConversation(data,contributer));
			$("#boardCommentList").show();
			
		});
	}
}
//*************************************************************************************************************
function getBoardDiscussionList(boardId){
	 var request = gapi.client.jb.board.listDiscussion({
		 'boardId' : boardId
	 });
	 request.execute(function(jsonResp, rawResp) {
		 console.log(jsonResp.code);
	 });
}
//*************************************************************************************************************
//get board information by URL at the beginning of loading 
function getBoardbyUrl(itemURL){
	var retVal = '';
	 var request = gapi.client.jb.board.itemurl.get({
		 'itemURL' : itemURL
	 });
	 request.execute(function(jsonResp, rawResp) {
		 if(jsonResp.id !=undefined){
			 $("#jcBoardId").val(jsonResp.id);
			 loadConversations(jsonResp.id);
			 loggedin();
		 }
		 else
			 $("#jcBoardId").val(0);
	 });
}
//*************************************************************************************************************
function getOwnerUserId(merchantId){
	console.log(merchantId);
	 var request = gapi.client.jb.merchant.get({
		 'id' : merchantId
	 });
	 request.execute(function(jsonResp, rawResp) {
		 $("#jcOwnerId").val(jsonResp.ownerUserId);
	 });
}
//*************************************************************************************************************
//this method is for adding a new friend to baord list after inserting a new comment.
//TODO:SA: Remember it should be changed after changing parameter name by BA. 
function addFriendBoard(boardId , fbId){
	 var request = gapi.client.jb.user.fb.get({
		 'id' : fbId
	 });
	 request.execute(function(jsonResp, rawResp) {
		 console.log(jsonResp.id);
		 console.log($("#jcOwnerId").val());
		 if(jsonResp.id !== $("#jcOwnerId").val()){
			 var request0 = gapi.client.jb.board.fb.addFriend({
				 'friendfbId' : fbId,
				 'id': boardId
			 });
			 request0.execute(function(jsonResp, rawResp) {
				 console.log(jsonResp.id);
			 });
		 }
	 });
}
